package testJdk.thread.testFuture;

import java.util.concurrent.*;

/**
 * @ClassName : learn_jdk.threadPoolTest.futureTest.FutureTest01
 * @Description : 类描述
 * Created by cyd on 2021-03-30 00:18:40
 */
public class FutureTest02 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 获得计时器
        ScheduledExecutorService clock = Executors.newScheduledThreadPool(2);
        ConcurrentHashMap<String, Integer> hashMap = new ConcurrentHashMap<>(3);
        hashMap.put("1", 1);
        // 将参数传入计时器中，调用任务修改hashMap，并获得返回值
        Future<ConcurrentHashMap<String, Integer>> result = clock.submit(new MyCall2(hashMap), hashMap);
        // 取出返回值
        ConcurrentHashMap<String, Integer> map = result.get();
        System.out.println(map.entrySet());
    }

}

class MyCall2 implements Runnable {

    ConcurrentHashMap<String, Integer> hashMap;

    public MyCall2(ConcurrentHashMap<String, Integer> hashMap) {
        this.hashMap = hashMap;
    }


    @Override
    public void run() {
        hashMap.put("2", 2);
        hashMap.remove("1");
    }
}